package com.example.taobaounion.presenter.impl;

import androidx.annotation.NonNull;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.domain.OnSellContent;
import com.example.taobaounion.presenter.IOnSellPagePresenter;
import com.example.taobaounion.utils.RetrofitUtils;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.callback.IOnSellPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnSellPagePresenter implements IOnSellPagePresenter {

    // Constant
    private static final int DEFAULT_PAGE = 1;


    private int mCurrentPage = DEFAULT_PAGE;
    private IOnSellPageCallback mViewCallback;
    private Api mApi;

    // flag
    private boolean mIsLoading = false;

    public OnSellPagePresenter() {
        Retrofit retrofit = RetrofitUtils.getInstance().getRetrofit();

        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getOnSellContent() {

        if (mIsLoading) {
            return;
        }

        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }

        String onSellPageUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);

        Call<OnSellContent> task = mApi.getOnSellPageContent(onSellPageUrl);

        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(@NonNull Call<OnSellContent> call, @NonNull Response<OnSellContent> response) {
                // change flag
                mIsLoading = false;


                int code = response.code();

                if (code == HttpURLConnection.HTTP_OK) {
                    OnSellContent content = response.body();

                    if (mViewCallback != null) {

                        try {
                            assert content != null;
                            int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();

                            if (size <= 0) {
                                mViewCallback.onEmpty();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            mViewCallback.onEmpty();
                        }

                        mViewCallback.onContentLoadedSuccess(content);
                    }
                } else {
                    if (mViewCallback != null) {
                        mViewCallback.onNetworkError();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<OnSellContent> call, @NonNull Throwable t) {
                // change flag
                mIsLoading = false;


                if (mViewCallback != null) {
                    mViewCallback.onNetworkError();
                }
            }
        });
    }

    @Override
    public void reLoad() {
        this.getOnSellContent();
    }

    @Override
    public void loaderMore() {

        if (mIsLoading) {
            return;
        }
        mIsLoading = true;

        mCurrentPage++;

        String onSellPageUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);

        Call<OnSellContent> task = mApi.getOnSellPageContent(onSellPageUrl);

        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(@NonNull Call<OnSellContent> call, @NonNull Response<OnSellContent> response) {
                // change flag
                mIsLoading = false;

                int code = response.code();

                if (code == HttpURLConnection.HTTP_OK) {
                    OnSellContent content = response.body();

                    if (mViewCallback != null) {

                        try {
                            assert content != null;
                            int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();

                            if (size <= 0) {
                                mCurrentPage--;
                                mViewCallback.onMoreLoadedEmpty();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            mCurrentPage--;
                            mViewCallback.onMoreLoadedError();
                        }

                        mViewCallback.onMoreLoaded(content);
                    }
                } else {
                    mCurrentPage--;
                    if (mViewCallback != null) {
                        mViewCallback.onMoreLoadedError();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<OnSellContent> call, @NonNull Throwable t) {
                // change flag
                mIsLoading = false;


                mCurrentPage--;
                if (mViewCallback != null) {
                    mViewCallback.onMoreLoadedError();
                }
            }
        });
    }

    @Override
    public void registerViewCallback(IOnSellPageCallback callback) {
        this.mViewCallback = callback;
    }

    @Override
    public void unRegisterViewCallback(IOnSellPageCallback callback) {
        mViewCallback = null;
    }
}
